package com.michaelwolf.mytool.util;

import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.List;

public class DirectoryTraverser {
	
	public static File[] getAllFilesFromDirectory(File dir, FilenameFilter filter){
		
		List<File> targetFilesList = new ArrayList<File>();
		
		traverserDir(dir, filter, targetFilesList);
		File[] targetFileArray = targetFilesList.toArray(new File[0]);
		
		return targetFileArray;
	}

	private static void traverserDir(File dir, FilenameFilter filter,
			List<File> targetFilesList) {
		for(File file : dir.listFiles()){
			if(file.isDirectory()){
				traverserDir(file, filter, targetFilesList);
			}else{
				if(filter.accept(file.getParentFile(), file.getName())){
					targetFilesList.add(file);
				}
			}
		}
		
	}

	public static void main(String[] args) {
		File[] files = DirectoryTraverser.getAllFilesFromDirectory(new File("C:\\src\\storage\\wcf\\java"), new FilenameFilter(){

			@Override
			public boolean accept(File dir, String name) {
				// TODO Auto-generated method stub
				return name.endsWith("java");
			}
			
		});
		for(File file : files){
			System.out.println(file.getName());
		}

	}

}
